<template>
  <div :class="className" :style="{height:height,width:width}" ></div>
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    },
    pieData: Object
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.initChart();

    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      // console.log('el',this.$el);
      this.chart = echarts.init(this.$el, "macarons");
      const baseOption = {
        color: ["#d3758f", "#f3d999", "#a4d8c2", "#ffb980"],
        title: {
          x: "center",
          y: "center",
          textStyle: {
            fontWeight: "normal",
            color: "#0580f2",
            fontSize: "16"
          }
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        series: [
          {
            type: "pie",
            radius: ["50%", "66%"],
            itemStyle: {
              normal: {
                label: {
                  show: false
                },
                labelLine: {
                  show: false
                }
              }
            },
            data: [
              // {
              //   itemStyle: {
              //     normal: {
              //       color: {
              //         // 完成的圆环的颜色
              //         colorStops: [
              //           {
              //             offset: 0,
              //             color: "#d3758f" // 0% 处的颜色
              //           },
              //           {
              //             offset: 1,
              //             color: "#f3d999" // 100% 处的颜色
              //           }
              //         ]
              //       }
              //     }
              //   }
              // }
            ]
          }
        ]
      };
      this.chart.setOption(baseOption);
      this.chart.setOption(this.pieData);
    }
  }
};
</script>
